/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.thrift.scoping;

import static org.eclipse.emf.common.util.URI.createURI;
import static org.eclipse.xtext.util.Tuples.pair;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.util.Pair;

import de.thwildau.tm.moses.common.util.ResourcesUtil;

/**
 * FileResolverStrategy that searches the path in a single directory.
 * 
 * @author Thomas Kistel
 * @version $Revision: 759 $
 */
class SingleDirectoryFileResolver implements FileResolverStrategy {

    private ResourcesUtil resources;


    SingleDirectoryFileResolver(ResourcesUtil resources) {
        this.resources = resources;
    }

    /*
     * (non-Javadoc)
     * @see de.thwildau.tm.moses.thrift.scoping.FileResolverStrategy#resolveUri(java.lang.String,
     * org.eclipse.emf.common.util.URI)
     */
    @Override
    public String resolveUri(String importUri, URI resourceUri) {

        IProject project = resources.project(resourceUri);
        // if (project == null) project = resources.activeProject();
        if (project == null)
            throw new IllegalStateException("Unable to find current project");
        // PathsPreferences preferences = preferencesFactory.preferences(project);
        // return resolver(preferences).resolveUri(importUri, resourceUri, preferences);
        return resolve(importUri, resourceUri);
    }

    public String resolve(String importUri, URI declaringResourceUri) {
        List<String> resourceUriSegments = removeFirstAndLast(declaringResourceUri.segmentsList());
        Pair<String, List<String>> importUriPair = pair(importUri, createURI(importUri).segmentsList());
        return resolveUri(importUriPair, resourceUriSegments);
    }

    // first is always "platform" and last is the file name (both unnecessary)
    private static List<String> removeFirstAndLast(List<String> list) {
        if (list.isEmpty())
            return list;
        List<String> newList = new ArrayList<String>(list);
        newList.remove(0);
        newList.remove(newList.size() - 1);
        return newList;
    }

    private String resolveUri(Pair<String, List<String>> importUri, List<String> resourceUri) {
        StringBuilder pathBuilder = new StringBuilder();
        String firstSegment = importUri.getSecond().get(0);
        for (String segment : resourceUri) {
            if (segment.equals(firstSegment))
                break;
            pathBuilder.append(segment).append(SEPARATOR);
        }
        String resolved = PLATFORM_RESOURCE_PREFIX + SEPARATOR + pathBuilder.toString() + importUri.getFirst();
        return (resources.fileExists(createURI(resolved))) ? resolved : null;
    }
}
